<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>公告管理</el-breadcrumb-item>
      <el-breadcrumb-item>公告内容</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片视图区域 -->
    <el-card>
      <!-- 搜索与添加区域 -->
      <el-row :gutter="20">
        <el-col :span="4">
          <el-input placeholder="请输入公告名称" v-model="notice.title" clearable></el-input>
        </el-col>
        <el-col :span="4">
          <el-input placeholder="请输入公告内容" v-model="notice.content" clearable></el-input>
        </el-col>
        <el-col :span="2">
          <el-button type="primary" icon="el-icon-search" @click="btnsearch">搜索</el-button>
        </el-col>
        <el-col :span="1">
          <el-button type="danger" icon="el-icon-search" @click="btndelete">删除</el-button>
        </el-col>
      </el-row>

      <!-- 用户列表(表格)区域 -->
      <el-table
        ref="multipleTable"
        :data="noticeList"
        tooltip-effect="dark"
        style="width: 100%"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55"></el-table-column>
        <el-table-column label="公告名称" prop="title"></el-table-column>
        <el-table-column label="公告内容" prop="content"></el-table-column>
        <el-table-column label="创建时间" >
          <template slot-scope="scope">{{ formatDate(scope.row.createDate) }}</template>
        </el-table-column>
        <el-table-column label="公告人" width="200" prop="username"></el-table-column>

        <el-table-column label="操作" width="220">
          <template slot-scope="scope">
            <!-- 编辑 -->
            <el-button
              type="primary"
              icon="el-icon-edit"
              size="mini"
              @click="editUserById(scope.row.id)"
            ></el-button>
            <!-- 预览 -->
            <el-button
              type="danger"
              icon="el-icon-search"
              size="mini"
              @click="showNotice(scope.row)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="pageNum"
        :page-sizes="[3,6, 9, 12]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>
    </el-card>


    <!-- Form 对话框-->

    <el-dialog title="预览公告" :visible.sync="dialogFormVisible">
      <el-form :model="form">
        <el-form-item label="公告标题" :label-width="formLabelWidth">
          <el-input v-model="form.title" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="公告内容" :label-width="formLabelWidth">
          <el-input v-model="form.content" auto-complete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="dialogFormVisible = false">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import moment from 'moment';//时间格式化

export default {
  created() {
    this.getNoticeList();
  },
  data() {
    return {
      //多选属性
      multipleSelection: [],
      //对话框需要的属性
      dialogFormVisible: false,
      form: {
        // title: "",
        // content: "",
      },
      formLabelWidth: '120px',
      //表格相关属性
      noticeList: [],
      total: 0,
      pageNum: 1,
      pageSize: 6,
      //notice对象(查询条件)
      notice: {
        title: "",
        content: "",
      }
    };
  },
  methods: {
    //删除公告
    getNoticeIds(){
      return this.multipleSelection.map(item => item.id)
    },
    btndelete(){
        this.$confirm('此操作将永久删除这些公告, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          const list=this.getNoticeIds()
          console.log(list)
          this.$http.delete("/notice/delete",{
        data: list,
        headers: {
        'Content-Type': 'application/json'
    }
      }).then(resp =>{
            // console.log(resp)
            if(resp.data.flag){
              this.$message({
            type: 'success',
            message: '删除成功!'
          });
          // this.queryparams.currentPage=1
          this.getNoticeList()
            }
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });          
        });
    
    },
    //时间格式化方法
    formatDate(date) {
        return moment(date).format('YYYY-MM-DD');
    },
    //分页公告
    async getNoticeList() {
      const { data: res } = await this.$http.post(
        `/notice/list?pageSize=${this.pageSize}&pageNum=${this.pageNum}`,
        this.notice
      );
      
      if (res.code == 200) {
        this.total = res.data.total;
        this.pageNum = res.data.pageNum;
        this.pageSize = res.data.pageSize;
        this.noticeList = res.data.list;
      } else {
        this.$message.error(res.message);
      }
    },
    handleSizeChange(v) {
        this.pageSize = v;
      this.getNoticeList();
    },
    handleCurrentChange(v) {
        this.pageNum = v;

      this.getNoticeList();
    },
    //搜索公告
    btnsearch() {
      this.getNoticeList();
    },
    
    //编辑公告
    editUserById(id) {
      this.$router.push({ path: "/noticeedit", query: { noticeid: id } });
    },
    //预览公告
    showNotice(row) {
      console.log(row);
      
      this.dialogFormVisible = true;
      this.form=row
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
    }
  }
};
</script>

<style></style>
